﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary1;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DataTable Source= Class1.SourceTable();
        DataTable GridViewDataSource=Class1.SourceTable();

        protected void Page_Init(object sender, EventArgs e)
        {
            GridView1.DataSource = GridViewDataSource;
            GridView1.DataBind();
            LabeledCombobox1.DataSource = GetOperations();
            LabeledCombobox1.DataBind();

            LabeledCombobox2.DataSource = GetDepartStations();
            LabeledCombobox2.DataBind();

            LabeledCombobox3.DataSource = GetArrivalStations();
            LabeledCombobox3.DataBind();

            LabeledCombobox4.DataSource = GetCurrentStations();
            LabeledCombobox4.DataBind();

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
           
           
        }
        protected List<object> GetOperations()
        {
            List<object> bufTable=new List<object>();

            bufTable.Add("<Все>");
            
            foreach (DataRow Row in Source.Rows)
            {
                if (!bufTable.Contains(Row[1]))
                {

                    bufTable.Add(Row[1]);
                }
            }

            return bufTable;
        }
        protected List<object> GetDepartStations()
        {
            List<object> bufTable = new List<object>();

            bufTable.Add("<Все>");

            foreach (DataRow Row in Source.Rows)
            {
                if (!bufTable.Contains(Row[2]))
                {

                    bufTable.Add(Row[2]);
                }

            }
            return bufTable;
        }
        protected List<object> GetArrivalStations()
        {
            List<object> bufTable = new List<object>();

            bufTable.Add("<Все>");

            foreach (DataRow Row in Source.Rows)
            {
                if (!bufTable.Contains(Row[3]))
                {

                    bufTable.Add(Row[3]);
                }

            }

            return bufTable;
        }
        protected List<object> GetCurrentStations()
        {
            List<object> bufTable = new List<object>();

            bufTable.Add("<Все>");

            foreach (DataRow Row in Source.Rows)
            {
                if (!bufTable.Contains(Row[4]))
                {

                    bufTable.Add(Row[4]);
                }

            }

            return bufTable;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<string> Filter = new List<string>();
 
            if (LabeledTextBox1.Text == "" )
                Filter.Add("*");
            else
                Filter.Add(LabeledTextBox1.Text);

            if (LabeledCombobox1.SelectedText == "<Все>")
                Filter.Add("*");
            else
                Filter.Add(LabeledCombobox1.SelectedText);

            if (LabeledCombobox2.SelectedText == "<Все>")
                Filter.Add("*");
            else
                Filter.Add(LabeledCombobox2.SelectedText);

            if (LabeledCombobox3.SelectedText == "<Все>")
                Filter.Add("*");
            else
                Filter.Add(LabeledCombobox3.SelectedText);

            if (LabeledCombobox4.SelectedText == "<Все>")
                Filter.Add("*");
            else
                Filter.Add(LabeledCombobox4.SelectedText);


            GridViewDataSource.Rows.Clear();
            foreach (DataRow row in Source.Select(String.Format("Convert([Номер],System.String) LIKE '{0}' AND [Операция] LIKE '{1}' AND [Станция отправления] LIKE '{2}' AND [Станция назначения] LIKE '{3}' AND [Станция операции] LIKE '{4}'",  Filter[0], Filter[1],Filter[2],Filter[3],Filter[4])))
            {
                GridViewDataSource.Rows.Add(row.ItemArray);
            }
            if (GridViewDataSource.Rows.Count == 0)
                GridViewDataSource.Rows.Add();
            GridView1.DataBind();
           
         
        }

        

    }
}